package com.wangan.wanganpush.xml;

import com.wangan.wanganpush.model.UserReg;
import com.wangan.wanganpush.props.MyProps;
import com.wangan.wanganpush.utils.AppConstants;
import com.wangan.wanganpush.utils.FileCreatUtils;
import com.wangan.wanganpush.utils.ImgDownloadUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 拼装用户注册数据的xml
 * xufm
 * <p>
 * 2019-01-10
 */
@Service
public class UserRegXml {

    @Resource
    MyProps myProps;

    public  String parseXml(List<UserReg> urList,String messageCode,String fileName) {
        if (urList == null || urList.size() == 0) return null;
        try {
            StringBuffer sb = new StringBuffer();
            sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>  <MESSAGE>");
            sb.append("<DATASET name=\"WA_COMMON_010000\">  <DATA>");
            sb.append("<ITEM key=\"H010001\" val=\"" + AppConstants.applicationCoding + "\"/>");
            sb.append("<ITEM key=\"I010014\" val=\"" + messageCode + "\"/>");
            sb.append("<ITEM key=\"I010031\" val=\"" + "ACCOUNT" + "\"/>");
            sb.append("<ITEM key=\"I010013\" val=\"" + 3 + "\"/>");
            sb.append("</DATA> </DATASET> ");

            sb.append("<DATASET name=\"WA_COMMON_010022\">");

            for (int i = 0; i < urList.size(); i++) {
                String filePath = myProps.getJarPath()+fileName;
                System.out.println(filePath);
                UserReg userReg = urList.get(i);
                sb.append("<DATA>");
                if (userReg.getUserAccount() != null) {
                    sb.append("<ITEM key=\"B040002\" val=\"" + userReg.getUserAccount() + "\"/>");
                }

                if (userReg.getUserIm() != null) {
                    sb.append("<ITEM key=\"B040040\" val=\"" + userReg.getUserIm() + "\"/>");
                }

                if (userReg.getUserId() != null) {
                    sb.append("<ITEM key=\"B040003\" val=\"" + userReg.getUserId() + "\"/>");
                }

                if (userReg.getPhone() != null) {
                    sb.append("<ITEM key=\"B020005\" val=\"" + userReg.getPhone() + "\"/>");
                }

                if (userReg.getEmail() != null) {
                    sb.append("<ITEM key=\"B040023\" val=\"" + userReg.getEmail() + "\"/>");
                }

                //用户头像
                if (userReg.getPassport() != null) {
                    String picureName = userReg.getPassport().split("/")[userReg.getPassport().split("/").length-1];
                    //创建临时文件夹
                    FileCreatUtils.creatFileDir(filePath);
                    //图片下载
                    ImgDownloadUtils.downloadPicture(userReg.getPassport(),filePath+"/"+picureName);

                    sb.append("<ITEM key=\"X900011\" val=\"" + userReg.getPassport() + "\" ismedia=\"true\"/>");
                }

                if (userReg.getPassword() != null) {
                    sb.append("<ITEM key=\"B040005\" val=\"" + userReg.getPassword() + "\"  fmt=\"base64\" />");
                }

                if (userReg.getNickName() != null) {
                    sb.append("<ITEM key=\"B040004\" val=\"" + userReg.getNickName() + "\"  fmt=\"base64\" />");
                }

                if (userReg.getRealName() != null) {
                    sb.append("<ITEM key=\"B010001\" val=\"" + userReg.getRealName() + "\"  fmt=\"base64\"/>");
                }

                if (userReg.getAge() != null) {
                    sb.append("<ITEM key=\"B010015\" val=\"" + userReg.getAge() + "\"/>");
                }

                if (userReg.getGender() != null) {
                    sb.append("<ITEM key=\"B010011\" val=\"" + userReg.getGender() + "\"/>");
                }

                if (userReg.getBirthData() != null) {
                    sb.append("<ITEM key=\"B010014\" val=\"" + userReg.getBirthData() + "\"/>");
                }

                if (userReg.getRegTime() != null) {
                    sb.append("<ITEM key=\"G020026\" val=\"" + userReg.getRegTime() + "\"/>");
                }

                if (userReg.getRegCity() != null) {
                    sb.append("<ITEM key=\"B030002\" val=\"" + userReg.getRegCity() + "\"/>");
                }

                if (userReg.getRegType() != null) {
                    sb.append("<ITEM key=\"B040008\" val=\"" + userReg.getRegType() + "\"/>");
                }

                if (userReg.getRegIp() != null) {
                    sb.append("<ITEM key=\"F020001\" val=\"" + userReg.getRegIp() + "\"/>");
                }

                if (userReg.getRegPort() != null) {
                    sb.append("<ITEM key=\"F020006\" val=\"" + userReg.getRegPort() + "\"/>");
                }

                if (userReg.getLandlinePhone() != null) {
                    sb.append("<ITEM key=\"B020004\" val=\"" + userReg.getLandlinePhone() + "\"/>");
                }

                if (userReg.getIdType() != null) {
                    sb.append("<ITEM key=\"B030004\" val=\"" + userReg.getIdType() + "\"/>");
                }

                if (userReg.getIdNumber() != null) {
                    sb.append("<ITEM key=\"B030005\" val=\"" + userReg.getIdNumber() + "\"/>");
                }


                if (userReg.getIdPhoto() != null) {
                    String picureName = userReg.getIdPhoto().split("/")[userReg.getIdPhoto().split("/").length-1];
                    //创建临时文件夹
                    FileCreatUtils.creatFileDir(filePath);
                    //图片下载
                    ImgDownloadUtils.downloadPicture(userReg.getIdPhoto(),filePath+"/"+picureName);
                    sb.append("<ITEM key=\"B030029\" val=\"" + userReg.getIdPhoto() + "\"  ismedia=\"true\"/>");
                }

                if (userReg.getAddress() != null) {
                    sb.append("<ITEM key=\"B020008\" val=\"" + userReg.getAddress() + "\"  fmt=\"base64\"/>");
                }

                if (userReg.getDomicile() != null) {
                    sb.append("<ITEM key=\"B030011\" val=\"" + userReg.getDomicile() + "\"  fmt=\"base64\"/>");
                }

                if (userReg.getBirthPlace() != null) {
                    sb.append("<ITEM key=\"B010026\" val=\"" + userReg.getBirthPlace() + "\"  fmt=\"base64\"/>");
                }

                if (userReg.getOccupation() != null) {
                    sb.append("<ITEM key=\"B030024\" val=\"" + userReg.getOccupation() + "\"  fmt=\"base64\"/>");
                }

                if (userReg.getUserType() != null) {
                    sb.append("<ITEM key=\"H230001\" val=\"" + userReg.getUserType() + "\"/>");
                }

                if (userReg.getCompany() != null) {
                    sb.append("<ITEM key=\"E010002\" val=\"" + userReg.getCompany() + "\"/>");
                }

                if (userReg.getDriverLicenceNumber() != null) {
                    sb.append("<ITEM key=\"C030901\" val=\"" + userReg.getDriverLicenceNumber() + "\"/>");
                }

                if (userReg.getDriverLincencePicture() != null) {
                    String picureName = userReg.getDriverLincencePicture().split("/")[userReg.getDriverLincencePicture().split("/").length-1];
                    //创建临时文件夹
                    FileCreatUtils.creatFileDir(filePath);
                    //图片下载
                    ImgDownloadUtils.downloadPicture(userReg.getDriverLincencePicture(),filePath+"/"+picureName);

                    sb.append("<ITEM key=\"C030902\" val=\"" + userReg.getDriverLincencePicture() + "\" ismedia=\"true\" />");
                }

                if (userReg.getActionType() != null) {
                    sb.append("<ITEM key=\"H010003\" val=\"" + userReg.getActionType() + "\"/>");
                }

                if (userReg.getActionTime() != null) {
                    sb.append("<ITEM key=\"I010005\" val=\"" + userReg.getActionTime() + "\"/>");
                }
                sb.append("</DATA>");
            }

            sb.append("</DATASET>");
            sb.append("</MESSAGE>");


            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }


  /*  public static void main(String[] args) {

        List<UserReg> urList = new ArrayList<>();
        UserReg u = new UserReg();
        u.setAddress("ceshi");
        u.setAge("11");
        u.setBirthPlace("长沙");
        u.setUserAccount("12306");
        u.setFlag(0);
        u.setUserId("1124");
        u.setUserType("1");
        urList.add(u);


        String str = parseXml(urList,"1234568754");
        System.out.println(str);
        StringToXml s = new StringToXml();
        s.stringToXml(str,"12354");
    }*/
}
